local skel = fk.CreateSkill {
  name = "lb__wuzhuang",
}

Fk:loadTranslationTable {
  ["lb__wuzhuang"] = "误撞",
  [":lb__wuzhuang"] = "每阶段限一次，当你使用牌时或成为牌的目标时，若你的手牌：<br>" ..
      "1.不少于体力上限，你可弃一张牌并令之伤害+1：<br>" ..
      "2.不多于体力值，你可摸一张牌；<br>" ..
      "若两项均执行，本回合结束时你对当前回合角色逆时针第X名角色造成1点伤害（X为你本回合弃置的牌数）。",

  ["@lb__wuzhuang-turn"] = "误撞",
  ["#lb__wuzhuang_dis"] = "误撞：你可以弃置一张牌并令 %arg 伤害+1",
  ["#lb__wuzhuang_draw"] = "误撞：你可以摸一张牌",

  ["$lb__wuzhuang1"] = "开始今天的工作吧。",
  ["$lb__wuzhuang2"] = "请、请让一下！",
}

local wuzhuang_spec = {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target and player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local n = {}
    if player:getHandcardNum() >= player.maxHp and #player.room:askToDiscard(target, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = skel.name,
          prompt = "#lb__wuzhuang_dis:::" .. data.card:toLogString(),
        }) > 0 then
      table.insert(n, 1)
    end
    if player:getHandcardNum() <= player.hp and room:askToSkillInvoke(player, {
          skill_name = skel.name,
          prompt = "#lb__wuzhuang_draw",
        }) then
      table.insert(n, 2)
    end
    event:setCostData(self, { n = n })
    return #n > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = event:getCostData(self).n
    if table.contains(n, 1) then
      data.additionalDamage = (data.additionalDamage or 0) + 1
      room:addTableMarkIfNeed(player, "lb__wuzhuang-turn", 1)
    end
    if table.contains(n, 2) then
      player:drawCards(1, skel.name)
      room:addTableMarkIfNeed(player, "lb__wuzhuang-turn", 2)
    end
  end,
}

skel:addEffect(fk.CardUsing, wuzhuang_spec)
skel:addEffect(fk.TargetConfirming, wuzhuang_spec)

skel:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and #player:getTableMark("lb__wuzhuang-turn") > 0 and
    player:getMark("@lb__wuzhuang-turn") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = player:getMark("@lb__wuzhuang-turn") % #room:getAlivePlayers()
    local t = room:getCurrent()
    if not t then return end
    if n > 0 then
      for _ = 1, n do
        t = t.next
      end
    end
    room:damage {
      from = player,
      to = room:getPlayerById(t.id),
      damage = 1,
      skillName = "lb__wuzhuang",
    }
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill("lb__wuzhuang", true) then return false end
    for _, m in ipairs(data) do
      if m.toArea == Card.DiscardPile and m.moveReason == fk.ReasonDiscard and m.from and m.proposer
          and m.from == m.proposer and m.from == player then
        local num = 0
        for _, info in ipairs(m.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            num = num + 1
          end 
        end
        if num > 0 then
          event:setCostData(self, {num = num})
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "@lb__wuzhuang-turn", event:getCostData(self).num)
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
    for _, m in ipairs(e.data) do
      if m.toArea == Card.DiscardPile and m.moveReason == fk.ReasonDiscard and m.from and m.proposer
          and m.from == m.proposer and m.from == player then
        player.room:addPlayerMark(player, "@lb__wuzhuang-turn", #table.filter(m.moveInfo, function(info)
          return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
        end))
      end
    end
  end, Player.HistoryTurn)
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@lb__wuzhuang-turn", 0)
end)

return skel
